﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.Common.IPQuery;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsComment
    {
        private readonly KeRui.DAL.CmsComment dal = new KeRui.DAL.CmsComment();

        public int Add(KeRui.Model.CmsComment model)
        {
            model.City = KeRui.Common.IPQuery.IPQuery.Web(Utils.GetRealIP()).City;
            return this.dal.Add(model);
        }

        public int Add2(KeRui.Model.CmsComment model)
        {
            return this.dal.Add(model);
        }

        public int AddBack(KeRui.Model.CmsComment model)
        {
            return this.dal.AddBack(model);
        }

        public int AddNodeComment(KeRui.Model.CmsComment model)
        {
            model.City = KeRui.Common.IPQuery.IPQuery.Web(Utils.GetRealIP()).City;
            return this.dal.AddNodeComment(model);
        }

        public List<KeRui.Model.CmsComment> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsComment> list = new List<KeRui.Model.CmsComment>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsComment item = new KeRui.Model.CmsComment();
                    if (dt.Rows[i]["CommentID"].ToString() != "")
                    {
                        item.CommentID = int.Parse(dt.Rows[i]["CommentID"].ToString());
                    }
                    if (dt.Rows[i]["AppID"].ToString() != "")
                    {
                        item.AppID = int.Parse(dt.Rows[i]["AppID"].ToString());
                    }
                    if (dt.Rows[i]["ContentID"].ToString() != "")
                    {
                        item.ContentID = int.Parse(dt.Rows[i]["ContentID"].ToString());
                    }
                    if (dt.Rows[i]["NodeID"].ToString() != "")
                    {
                        item.NodeID = int.Parse(dt.Rows[i]["NodeID"].ToString());
                    }
                    if (dt.Rows[i]["SiteID"].ToString() != "")
                    {
                        item.SiteID = int.Parse(dt.Rows[i]["SiteID"].ToString());
                    }
                    if (dt.Rows[i]["PublicID"].ToString() != "")
                    {
                        item.PublicID = int.Parse(dt.Rows[i]["PublicID"].ToString());
                    }
                    if (dt.Rows[i]["UserID"].ToString() != "")
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    item.Username = dt.Rows[i]["Username"].ToString();
                    item.City = dt.Rows[i]["City"].ToString();
                    item.Content = dt.Rows[i]["Content"].ToString();
                    if (dt.Rows[i]["SupportNum"].ToString() != "")
                    {
                        item.SupportNum = int.Parse(dt.Rows[i]["SupportNum"].ToString());
                    }
                    if (dt.Rows[i]["AgainstNum"].ToString() != "")
                    {
                        item.AgainstNum = int.Parse(dt.Rows[i]["AgainstNum"].ToString());
                    }
                    if (dt.Rows[i]["IsDelete"].ToString() != "")
                    {
                        item.IsDelete = int.Parse(dt.Rows[i]["IsDelete"].ToString());
                    }
                    if (dt.Rows[i]["floor"].ToString() != "")
                    {
                        item.floor = int.Parse(dt.Rows[i]["floor"].ToString());
                    }
                    item.QuoteID = dt.Rows[i]["QuoteID"].ToString();
                    item.QuoteUser = dt.Rows[i]["QuoteUser"].ToString();
                    if (dt.Rows[i]["QuoteTime"].ToString() != "")
                    {
                        item.QuoteTime = DateTime.Parse(dt.Rows[i]["QuoteTime"].ToString());
                    }
                    item.QuoteContent = dt.Rows[i]["QuoteContent"].ToString();
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int CommentID)
        {
            this.dal.Delete(CommentID);
        }

        public int GetCount(string strCondition)
        {
            string strWhere = "";
            if (strCondition != "")
            {
                strWhere = strWhere + strCondition;
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("CmsComment", strWhere);
        }

        public int GetCount(int IsDelete, int ContentID)
        {
            string str = " ";
            if (IsDelete != -1)
            {
                object obj2 = str;
                str = string.Concat(new object[] { obj2, " AND IsDelete = ", IsDelete, "" });
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("CmsComment", string.Concat(new object[] { " ContentID=", ContentID, " ", str }));
        }

        public int GetCount(int IsDelete, int IsRep, int siteID, int AppID, int NodeID, int ContentID)
        {
            object obj2;
            string str = "";
            if (IsDelete != -1)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and IsDelete = ", IsDelete, "" });
            }
            if (IsRep == -1)
            {
                str = str + " and QuoteID = ''";
            }
            else if (IsRep == 1)
            {
                str = str + " and IsDelete <>''";
            }
            if (NodeID != -1)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and ContentID IN (SELECT ContentID FROM CmsContent WHERE NodeID = ", NodeID, "  and SiteID = ", siteID, ")" });
            }
            if (ContentID != -1)
            {
                str = str + " and ContentID  = " + ContentID;
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("CmsComment", string.Concat(new object[] { " siteID=", siteID, " and AppID =", AppID, "", str }));
        }

        public List<KeRui.Model.CmsComment> GetList()
        {
            string strWhere = " ContentID  IN (select top 14 ContentID from CmsComment order by ContentID desc)";
            string strOrder = " ";
            DataSet set = new KeRui.DAL.Common().GetList("CmsComment", 0, 0, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsComment> GetList(int PageSize, int PageIndex, int IsDelete, int ContentID)
        {
            string str = "";
            if (IsDelete != -1)
            {
                object obj2 = str;
                str = string.Concat(new object[] { obj2, " AND IsDelete = ", IsDelete, "" });
            }
            DataSet set = new KeRui.DAL.Common().GetList("CmsComment", PageSize, PageIndex, string.Concat(new object[] { " ContentID=", ContentID, " ", str }), " CommentID ");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsComment> GetList(int PageSize, int PageIndex, int IsDelete, int ContentID, int NodeId)
        {
            string str = "";
            if (IsDelete != -1)
            {
                object obj2 = str;
                str = string.Concat(new object[] { obj2, " AND IsDelete = ", IsDelete, " AND NodeID='", NodeId, "'" });
            }
            DataSet set = new KeRui.DAL.Common().GetList("CmsComment", PageSize, PageIndex, string.Concat(new object[] { " ContentID=", ContentID, " ", str }), " CommentID ");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsComment> GetList(int PageSize, int PageIndex, int IsDelete, int IsRep, int siteID, int AppID, int NodeID, int ContentID)
        {
            object obj2;
            string str = "";
            if (IsDelete != -1)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and IsDelete = ", IsDelete, "" });
            }
            if (IsRep == -1)
            {
                str = str + " and QuoteID = ''";
            }
            else if (IsRep == 1)
            {
                str = str + " and IsDelete <>''";
            }
            if (NodeID != -1)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and ContentID IN (SELECT ContentID FROM CmsContent WHERE NodeID = ", NodeID, "  and SiteID = ", siteID, ")" });
            }
            if (ContentID != -1)
            {
                str = str + " and ContentID  = " + ContentID;
            }
            DataSet set = new KeRui.DAL.Common().GetList("CmsComment", PageSize, PageIndex, string.Concat(new object[] { " siteID=", siteID, " and AppID =", AppID, "", str, "" }), " CommentID Desc");
            return this.DataTableToList(set.Tables[0]);
        }

        public DataTable GetListByWhereOrder(int PageSize, int PageIndex, string strCondition, string strOrder)
        {
            string strWhere = "";
            if (strCondition != "")
            {
                strWhere = strWhere + strCondition;
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetList("CmsComment", PageSize, PageIndex, strWhere, strOrder).Tables[0];
        }

        public KeRui.Model.CmsComment GetModel(int CommentID)
        {
            return this.dal.GetModel(CommentID);
        }

        public KeRui.Model.CmsComment GetModelByCache(int CommentID)
        {
            string cacheKey = "CmsCommentModel-" + CommentID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(CommentID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsComment) cache;
        }

        public void Remove(int CommentID)
        {
            this.dal.Remove(CommentID);
        }

        public int UpdateForSupportOrAgainst(int CommentID, int Flag, string CreateIP)
        {
            return this.dal.UpdateForSupportOrAgainst(CommentID, Flag, CreateIP);
        }
    }
}

